﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Noris.Tools.TraceVisualiser.Components;
using Noris.Tools.TraceVisualiser.Support;

namespace Noris.Tools.TraceVisualiser.Components
{
    public partial class DForm : Form
    {
        public DForm()
        {
            InitializeComponent();
        }
        #region Tooltip
        /// <summary>
        /// Druh tooltipu
        /// </summary>
        [Category("DForm")]
        [Description("Zjednodušené nastavení vlastností tooltipu")]
        public TooltipMode CurrentTooltipMode
        {
            get { return this.ToolTip.CurrentTooltipMode; }
            set { this.ToolTip.CurrentTooltipMode = value; }
        }
        /// <summary>
        /// Objekt tooltipu
        /// </summary>
        [Category("DForm")]
        [Description("Objekt tooltipu pro plné nastavení jeho vlastností")]
        public DToolTip ToolTipForm { get { return this.ToolTip; } }
        /// <summary>
        /// Pozice a stav okna
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public DFormPosition FormPosition
        {
            get { return DFormPosition.Create(this); }
            set { if (value != null) value.ApplyTo(this); }
        }
		#endregion
    }
    /// <summary>
    /// Třída pro uložení pozice okna
    /// </summary>
    public class DFormPosition
    {
        private DFormPosition() { }
        private DFormPosition(Rectangle formBounds, FormWindowState windowState)
        {
            this.FormBounds = formBounds;
            this.WindowState = windowState;
        }
        public Rectangle FormBounds { get; set; }
        public FormWindowState WindowState { get; set; }
        public override string ToString()
        {
            return this.WindowState.ToString() + "; Bounds: " + this.FormBounds.ToString();
        }
        public string Serial
        {
            get
            {
                return Convertor.RectangleToString(this.FormBounds) + ":" +
                       Convertor.EnumToString<FormWindowState>(this.WindowState);
            }
            set
            {
                if (String.IsNullOrEmpty(value)) return;
                string[] items = value.Split(':');
                if (items.Length == 2)
                {
                    this.FormBounds = (Rectangle)Convertor.StringToRectangle(items[0]);
                    this.WindowState = (FormWindowState)Convertor.StringToEnum<FormWindowState>(items[1], FormWindowState.Normal);
                }
            }
        }
        public static DFormPosition Create(string serial)
        {
            if (String.IsNullOrEmpty(serial)) return null;

            DFormPosition formPosition = new DFormPosition();
            formPosition.Serial = serial;
            return formPosition;
        }
        public static DFormPosition Create(Form form)
        {
            if (form == null) return null;

            FormWindowState windowState = form.WindowState;
            if (windowState == FormWindowState.Maximized || windowState == FormWindowState.Minimized)
                return new DFormPosition(form.RestoreBounds, windowState);
            return new DFormPosition(form.DesktopBounds, windowState);
        }
        public void ApplyTo(Form form)
        {
            if (form != null)
            {
                if (this.WindowState == FormWindowState.Maximized || this.WindowState == FormWindowState.Minimized)
                {
                    form.WindowState = FormWindowState.Normal;
                    form.DesktopBounds = this.FormBounds;
                    form.WindowState = this.WindowState;
                }
                else
                {
                    form.WindowState = this.WindowState;
                    form.DesktopBounds = this.FormBounds;
                }
            }
        }
    }
}
